package dao.impl;

import Utils.C3P0Utils;
import domain.Product;
import dao.ProductDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/2/25 9:22
 */
public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> findAll() throws SQLException {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="select * from product";
        List<Product>list = runner.query(sql,new BeanListHandler<Product>(Product.class));
        return list;
    }

    @Override
    public void delete(String pid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="delete from product where pid=?";
        try {
            runner.update(sql, pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void add(Product product) throws SQLException {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSource());
        String sql="INSERT INTO product(pname,marketprice,shopprice,ishot,pdesc) VALUES(?,?,?,?,?)";
        runner.update(sql,new Object[]{product.getPname(),product.getMarketprice(),product.getShopprice(),product.getIshot(),product.getPdesc()});

    }



}
